package net.csdn.business.common.utils;

import net.csdn.business.common.constant.CommonConstants;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author likun
 * @date 2022/6/8 11:03
 */
public class IpUtil {
    private static final String IP = "127.0.0.1";

    public static String getRequestIp() {
        if (ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes())) {
            return IP;
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getRemoteAddr();
    }

    public static String getIp() {
        try {
            if (ObjectUtils.isEmpty(RequestContextHolder.getRequestAttributes())) {
                return getStaticIp();
            }
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            // 优先获取traceIp
            String traceIp = request.getHeader(CommonConstants.TRACE_IP);
            if (StringUtils.isNotBlank(traceIp)) {
                return traceIp;
            }

            String ip = request.getHeader("x-forwarded-for");
            if (ip != null && ip.length() != 0 && !CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                // 多次反向代理后会有多个ip值，第一个ip才是真实ip
                if (ip.contains(CommonConstants.COMMA)) {
                    ip = ip.split(CommonConstants.COMMA)[0];
                }
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        } catch (Exception ignore) {
            return getStaticIp();
        }
    }


    public static String getIp(ServerHttpRequest request) {
        String ip = "";
        try {
            HttpHeaders headers = request.getHeaders();
            ip = headers.getFirst("x-forwarded-for");
            if (ip != null && ip.length() != 0 && !CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                // 多次反向代理后会有多个ip值，第一个ip才是真实ip
                if (ip.contains(CommonConstants.COMMA)) {
                    ip = ip.split(CommonConstants.COMMA)[0];
                }
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = headers.getFirst("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = headers.getFirst("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = headers.getFirst("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = headers.getFirst("X-Real-IP");
            }
            if (ip == null || ip.length() == 0 || CommonConstants.TEXT_UNKNOWN.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddress().getAddress().getHostAddress();
            }
            return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
        } catch (Exception ignore) {
            return getStaticIp();
        }

    }

    public static String getStaticIp() {
        return IP;
    }

}